package oop;

import javax.swing.*;
import java.math.BigDecimal;

/**
 * @author dxm
 */
public class Wallet {
    private String id;
    private long createTime;
    private BigDecimal balance;
    private long balanceLastModifiedTime;

    public Wallet() {
        this.id = this.generateId();
        this.createTime = System.currentTimeMillis();
        this.balance = BigDecimal.ZERO;
        this.balanceLastModifiedTime = System.currentTimeMillis();
    }
    public String generateId(){
        return String.valueOf(Math.random()) ;
    }
    public String getId() {
        return this.id;
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public long getBalanceLastModifiedTime() {
        return this.balanceLastModifiedTime;
    }

    public void increaseBalance(BigDecimal increaseBalance) throws Exception {
        if (increaseBalance.compareTo(BigDecimal.ZERO) < 0) {
            throw new Exception("金额不能小于0");
        }
        this.balance.add(increaseBalance);
        this.balanceLastModifiedTime = System.currentTimeMillis();
    }

    public void decreaseBalance(BigDecimal decreaseAmount) throws Exception {
        if (decreaseAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new Exception("金额不能小于0");
        }
        if (decreaseAmount.compareTo(this.balance) > 0) {
            throw new Exception("减少的金额超过余额");
        }
        this.balance.subtract(decreaseAmount);
        this.balanceLastModifiedTime = System.currentTimeMillis();
    }
}
